Context-free language

In formal language theory, a context-free language is a language generated by some context-free grammar. The set of all context-free languages is identical to the set of languages accepted by pushdown automata.

Contents

Examples

An archetypical context-free language is L = \{a^nb^n:n\geq1\}, the language of all non-empty even-length strings, the entire first halves of which are a's, and the entire second halves of which are b's. L is generated by the grammar S\to aSb ~|~ ab, and is accepted by the pushdown automaton M=(\{q_0,q_1,q_f\}, \{a,b\}, \{a,z\}, \delta, q_0, \{q_f\}) where \delta is defined as follows:

\delta(q_0, a, z) = (q_0, a)
\delta(q_0, a, a) = (q_0, a)
\delta(q_0, b, a) = (q_1, x)
\delta(q_1, b, a) = (q_1, x)
\delta(q_1, \lambda, z) = (q_f, z)

\delta(\mathrm{state}_1, \mathrm{read}, \mathrm{pop}) = (\mathrm{state}_2, \mathrm{push})
where z is initial stack symbol and x means pop action.

Context-free languages have many applications in programming languages; for example, the language of all properly matched parentheses is generated by the grammar S\to SS ~|~ (S) ~|~ \lambda. Also, most arithmetic expressions are generated by context-free grammars.

Closure properties

Context-free languages are closed under the following operations. That is, if L and P are context-free languages, the following languages are context-free as well:

Context-free languages are not closed under complement, intersection, or difference. However, if L is a context-free language and D is a regular language then both their intersection L\cap D and their difference L\setminus D are context-free languages.

Nonclosure under intersection and complement

The context-free languages are not closed under intersection. This can be seen by taking the languages A = \{a^n b^n c^m \mid m, n \geq 0 \} and B = \{a^m b^n c^n \mid m,n \geq 0\}, which are both context-free. Their intersection is A \cap B = \{ a^n b^n c^n \mid n \geq 0\}, which can be shown to be non-context-free by the pumping lemma for context-free languages.

Context-free languages are also not closed under complementation, as for any languages A and B: A \cap B = \overline{\overline{A} \cup \overline{B}} .

Decidability properties

The following problems are undecidable for arbitrary context-free grammars A and B:

The following problems are decidable for arbitrary context-free languages:

Properties of context-free languages

References